<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Edit extends CI_Controller {
function __construct()
    {
        parent::__construct();
    	$this->load->library('session');
		$this->load->helper('url');
    	$this->load->database();
    	$this->load->model("centermod");
    	
	}
	
function edittours()
	{	
	if($this->session->userdata("user")==false && $this->session->userdata("level")!="admin")
			{
					
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$idtour=$this->input->get_post("idtour");
				if($idtour)
				{
				$detailtour=$this->centermod->ltourbyid($idtour);
				$detailforfaitsfortours=$this->centermod->lforfaitbyid($idtour);
				$data["edit"]=$detailtour;
				$data["forfaits"]=$detailforfaitsfortours;	
				$this->load->view("tour",$data);				
				}else
				{
					header('Location:index.php?liste/listtour');
				} 			
										
			}
	}
function editbus()
	{	
	if($this->session->userdata("user")==false && $this->session->userdata("level")!="admin")
			{
					
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$idbus=$this->input->get_post("idbus");
				if($idbus)
				{
				$detailbus=$this->centermod->infobusbyid($idbus);
				$data["edit"]=$detailbus;	
				$this->load->view("bus",$data);				
				}else
				{
					header('Location:index.php?liste/listtour');
				} 			
										
			}
	}
	
	
function actionEdit($type)
			{
			//$this->session->unset_userdata('user');
		if($this->session->userdata("user")==false && $this->session->userdata("level")!="admin")
			{
					
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{

				switch ($type)
				{
				 case "tour":
      						  $idtour=$this->input->get_post("idtour"); 
				 			  $TourName=$this->input->get_post("TourName"); 
      						  $TourCode=$this->input->get_post("TourCode"); 
      						  $Date=$this->input->get_post("Date"); 
      						  $time=$this->input->get_post("time");
      						  $lieu=$this->input->get_post("lieu");  
      						  $Description=$this->input->get_post("Description");
      						  //$fixedprice=$this->input->get_post("fixedprice");
      						   $forfaittype=$this->input->get_post("chambreyesno");
      						  $data=array( "name"=>$TourName,
      						  			   "code"=>$TourCode,
      						  			   "Date"=>$Date,
										   "Description"=>$Description,
      						  			   "forfaittype"=>$forfaittype,
      						  			   "time"=>$time,
      						  			   "lieu"=>$lieu
      						  				);
      						  $where="idtours=$idtour";
      						 
      						  if($TourName!=""&&$TourCode!=""&&$Date!=""&&$Description!=""&&$idtour!="")
      						  
      						  {
								 $idinsert=$this->centermod-> updatetemplate("tours",$data,$where);
      						  	if($forfaittype==0)
								{
									
									$chambretype=0;
									$title=$this->input->get_post("c0ttle"); 
									$priceadult=$this->input->get_post("c0adult"); 
									$pricechild=$this->input->get_post("c0child"); 
									$data2=array("title"=>$title,"priceadult"=>$priceadult,"pricechild"=>$pricechild);
									$where0="fk_idtours=$idtour and chambretype=$chambretype";
									$idinsert_f=$this->centermod-> updatetemplate("forfaits",$data2,$where0);
									for($i=1;$i<5;$i++)
									{
										$chambretype=$i;
										$title=""; 
										$priceadult=0; 
										$pricechild=0; 
										$data_ch=array("title"=>$title,"priceadult"=>$priceadult,"pricechild"=>$pricechild);
										$where_tmp="fk_idtours=$idtour and chambretype=$chambretype";
										$idinsert_f=$this->centermod-> updatetemplate("forfaits",$data_ch,$where_tmp);
										
									}
								}
							if($forfaittype==1)
								{
									
									
									$title=""; 
									$priceadult=0; 
									$pricechild=0; 
									$data_ch=array("title"=>$title,"priceadult"=>$priceadult,"pricechild"=>$pricechild);
									$where_0="fk_idtours=$idtour and chambretype=0";
									$idinsert_f=$this->centermod-> updatetemplate("forfaits",$data_ch,$where_0);
									for($i=1;$i<5;$i++)
									{
									$chambretype=$i;
									$title=$this->input->get_post("c".$i."ttle"); 
									$priceadult=$this->input->get_post("c".$i."adult"); 
									$pricechild=$this->input->get_post("c".$i."child"); 
									$data_ch=array("title"=>$title,"priceadult"=>$priceadult,"pricechild"=>$pricechild);
									$where_tmp="fk_idtours=$idtour and chambretype=$chambretype";
									$idinsert_f=$this->centermod-> updatetemplate("forfaits",$data_ch,$where_tmp);
									}
								
								}							
      						  	
      						  	    						  	
								//$data=array("message"=>"The tour is Added");
      						 		header("Location:index.php?liste/listtour");
      						  } else 
      						  {
      						  		header('Location:index.php?liste/listtour');
      						  
      						  }	
      						  
      						 
      						 	break;	
      						 	
      					 case "bus":
      					 	  $idbus=$this->input->get_post("idbus"); 
      						  $codebus=$this->input->get_post("codebus"); 
      						  $seatquantity=$this->input->get_post("seatquantity"); 
      						  $Guider=$this->input->get_post("Guider"); 
      						  $Driver=$this->input->get_post("Driver"); 
      						  $Bus_Company=$this->input->get_post("BusCompany"); 
      						  $hotelinfo=$this->input->get_post("HotelInfo"); 
      						  $fk_idtours=$this->input->get_post("idtour");
      						  $seatavaible=$seatquantity;
      						  
      						   
      						  $data=array( "code"=>$codebus,
      						  			   "seatqty"=>$seatquantity,
      						  				"seatavaible"=>$seatavaible,
      						  			   "Driver"=>$Driver,
      						  			   "Guider"=>$Guider,
										   "Bus_Company"=>$Bus_Company,
      									   "hotel_info"=>$hotelinfo,
      						               "fk_idtours"=>$fk_idtours
      						  );
      						   $where="idbus=$idbus";
      						  if($codebus!=""&$seatquantity!=""&&$hotelinfo=!"")
      						  {
      						 	$idinsert=$this->centermod-> updatetemplate("bus",$data,$where);
								header("Location:index.php?liste/listtour");
      						  } else 
      						  {
      						  	header('Location:index.php?liste/listtour');
      						  	
      						  }	
      						 	break;
      						 	
      						 	
				}//switch
			}//session
				
			}//actionedit
			
			
// edit pwd and email and level
function edituserform()
{
		if($this->session->userdata("user")==false ||$this->session->userdata("level")!="admin")
			{
					
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$iduser=$this->input->get_post("idusers"); 
				if($iduser>0)
				{
					$infouser=$this->centermod->getuserinfobyid($iduser);
					$data=array("infouser"=>$infouser,"type"=>"edit");
					$this->load->view("edituserform",$data);	
				}
			}
			

}

function actionedituser()
	{
		
		if($this->session->userdata("user")==false || $this->session->userdata("level")!="admin")
			{
					
					header('Location:index.php?pages/loginadmin');
						
			}else {
		$idusers=$this->input->get_post("idusers"); 
		$pwd=$this->input->get_post("pwd"); 
		$email=$this->input->get_post("email"); 
		$level=$this->input->get_post("level"); 
		IF($idusers>0)
		{
			if($pwd!="")
			{
				$data=array("pwd"=>$pwd,"email"=>$email,"level"=>$level);
			}else
			{
				$data=array("email"=>$email,"level"=>$level);
			}
		$where="idusers=$idusers";
		$this->centermod->updatetemplate("users",$data,$where);
		redirect('index.php?liste/listuser', 'refresh');
		 
							
		}
			}
	}

	
/** add new function 2012-11-18 xiaoming **/
// change no paid to paid

	function actionpaid()
	{
		
		if($this->session->userdata("user")==false)
			{
					
					header('Location:index.php?pages/loginadmin');
						
			}else
			 {
					$idtour=$this->input->get_post("idtour"); 
					$idbus=$this->input->get_post("idbus"); 
					$frompage=$this->input->get_post("frompage"); 
					$idinscriptions=$this->input->get_post("idinscriptions");
					if($idinscriptions>0)
					{
					  $this->centermod->changepaid($idinscriptions);
					  $infoallforinvoice=$this->centermod->allinfoforinvoice($idinscriptions);
					  $balance=$infoallforinvoice[0]["balance"];
					  $orginalprice=$infoallforinvoice[0]["orginalprice"];
					  $salesprice=$infoallforinvoice[0]["salesprice"];
					  $prixadditonal=$infoallforinvoice[0]["prixadditonal"];
					  $idinvoice=$infoallforinvoice[0]["idinvoice"];
					  if($orginalprice>0)
					  {
					  	$addtionpricefinal=$balance+$salesprice-$orginalprice+$prixadditonal;
					  	$uorginalprice=array("salesprice"=>$orginalprice);
					  	$this->centermod->updatetemplate("inscriptions",$uorginalprice,"idinscriptions=$idinscriptions");
					  	
					  	//
					  	$uinvoice=array("balance"=>0,"prixadditonal"=>$addtionpricefinal);
					  	$this->centermod->updatetemplate("invoice",$uinvoice,"idinvoice=$idinvoice");
					  }
					}
					header("Location:index.php?liste/listdetail/$idbus/$idtour");
			 	
			 }
	}
	
	function  getremider()
	{
		if($this->session->userdata("user")==false)
			{
					header('Location:index.php?pages/loginadmin');
			}
			else
			{
				$idinscriptions=$this->input->get_post("idinscriptions"); 
				$idtour=$this->input->get_post("idtour"); 
				$idbus=$this->input->get_post("idbus");
				$reminder_array=$this->centermod->getreminderbyid($idinscriptions);
				if(count($reminder_array)==1)
				{
					$reminder=$reminder_array[0]["reminder"];
					$data=array("reminder"=>$reminder,"idinscriptions"=>$idinscriptions,"idtour"=>$idtour,"idbus"=>$idbus);
					$this->load->view("reminder",$data);
				}
				else 
				{
					echo "error 1012 not find reminder";
				}
				
			}
	
	}
	
	function changereminder()
	{
		if($this->session->userdata("user")==false)
			{
					header('Location:index.php?pages/loginadmin');
			}
			else
			{	
				$idinscriptions=$this->input->get_post("idinscriptions"); 
				$idtour=$this->input->get_post("idtour"); 
				$idbus=$this->input->get_post("idbus");
				$reminder=$this->input->get_post("reminder");
				$data=array("reminder"=>$reminder);
				$this->centermod->updatetemplate("inscriptions",$data,"idinscriptions=$idinscriptions");
				header("Location:index.php?liste/listdetail/$idbus/$idtour");
				
			}
			
			
	}
	

/****************** party for partner module   ************************/
	function editpartnerform()
{
		if($this->session->userdata("user")==false ||$this->session->userdata("level")!="admin")
			{
					
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$iduser=$this->input->get_post("idpartners"); 
				if($iduser>0)
				{
					$infouser=$this->centermod->getpartnersinfobyid($iduser);
					$data=array("infouser"=>$infouser,"type"=>"edit");
					$this->load->view("partners/editpartnerform",$data);	
				}
			}
			

}

function actioneditpartner()
	{
		
		if($this->session->userdata("user")==false || $this->session->userdata("level")!="admin")
			{
					
					header('Location:index.php?pages/loginadmin');
						
			}else {
		$idpartners=$this->input->get_post("idpartners"); 
		$pwd=$this->input->get_post("pwd"); 
		$address=$this->input->get_post("address"); 
		$tel=$this->input->get_post("tel"); 
		$fax=$this->input->get_post("fax"); 
		$email=$this->input->get_post("email"); 
		$active=$this->input->get_post("active"); 
		$sptype=$this->input->get_post("sptype"); 
		IF($idpartners>0)
		{
			if($pwd!="")
			{
				$data=array("pwd"=>$pwd,"address"=>$address,"tel"=>$tel,"fax"=>$fax,"active"=>$active,"email"=>$email,"sptype"=>$sptype);
			}else
			{
				$data=array("address"=>$address,"tel"=>$tel,"fax"=>$fax,"active"=>$active,"email"=>$email,"sptype"=>$sptype);
			}
		$where="idpartners=$idpartners";
		$this->centermod->updatetemplate("partners",$data,$where);
		redirect('index.php?liste/listpartners', 'refresh');
		 
							
		}
			}
	}
	
	
/*********************************************************************/		
}//eidt class